package com.cci.kangdao.task;


import com.alibaba.fastjson.JSON;
import com.cci.kangdao.crmapi.CRMApiUtils;
import com.cci.kangdao.crmapi.CRMResponeEntity;
import com.cci.kangdao.dao.CRMPlatformServiceDao;
import com.cci.kangdao.dao.ExceptionDataSyncCrmTDao;
import com.cci.kangdao.redis.RedisClientUtils;
import com.cci.kangdao.utilTool.CollectionUtils;
import com.cci.kangdao.utilTool.LocalTimeUtil;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Component
public class ExceptionDataSyncCrmTask {
    private Logger log = Logger.getLogger(this.getClass().getName());

    @Resource
    private ExceptionDataSyncCrmTDao exceptionDataSyncCrmTDao;
    @Autowired
    private CRMPlatformServiceDao platformServiceDao;
    @Autowired
    private RedisClientUtils redisClientUtils;


    //因为服务器时间比现实时间早八小时，所以这里设置每天3点30和15点30定时执行(相当于现实时间的11点30和23点30定时执行)
    public void exceptionDataSyncCrm(){
        log.info("定时任务，同步srt确认维修数据到crm开始：当前时间为:"+ LocalTimeUtil.getRealCurrentTime());
        try {
            List<Map<String,Object>> exceptionDataSyncCrmTSyncFailureList = exceptionDataSyncCrmTDao.getExceptionDataSyncCrmTSyncFailureList();
            if(CollectionUtils.isNotEmpty(exceptionDataSyncCrmTSyncFailureList)){
                exceptionDataSyncCrmTSyncFailureList.stream().forEach(exceptionDataSyncCrmTSyncFailure->{
                    String mapStr = MapUtils.getString(exceptionDataSyncCrmTSyncFailure,"ExceptionData","");
                    if(StringUtils.isNotEmpty(mapStr)){
                        Map<String,Object> contentJsonMap = JSON.parseObject(mapStr);
                        CRMResponeEntity CRMres = CRMApiUtils.getCRMServiceApi().syncRepairAckToCRM("CCI",platformServiceDao,redisClientUtils,contentJsonMap);
                        Map<String,Object> updateExceptionDataSyncCrmTMap = new HashMap<>();
                        updateExceptionDataSyncCrmTMap.put("Updator",-1);
                        updateExceptionDataSyncCrmTMap.put("UpdateTime",LocalTimeUtil.getRealCurrentTime());
                        updateExceptionDataSyncCrmTMap.put("ID",MapUtils.getLongValue(exceptionDataSyncCrmTSyncFailure,"ID"));
                        if(CRMres.getStatus()!=0) {
                            String errorMsg = "同步确认维修信息至CRM失败，状态："+CRMres.getStatus()+"，用户信息:"+CRMres.getMessage();
                            updateExceptionDataSyncCrmTMap.put("FailReason",errorMsg);
                            //APP同步工单信息到CRM系统失败
                            log.error(errorMsg);
                        }else{
                            updateExceptionDataSyncCrmTMap.put("IsSyncSuccess",1);
                        }
                        exceptionDataSyncCrmTDao.update(updateExceptionDataSyncCrmTMap);
                    }
                });
            }
        } catch (Exception e) {
            log.error("定时任务，同步srt确认维修数据到crm发生异常，异常信息" + e.getMessage(), e);
        }
        log.info("同步srt确认维修数据到crm定时任务完毕!");
    }




}
